package com.sportswork.sportswork.controller.admin;

import com.sportswork.sportswork.config.SecurityUserInfo;
import com.sportswork.sportswork.core.entity.Role;
import com.sportswork.sportswork.core.entity.Student;
import com.sportswork.sportswork.core.entity.Teacher;
import com.sportswork.sportswork.core.entity.User;
import com.sportswork.sportswork.core.service.dto.PageDTO;
import com.sportswork.sportswork.core.service.dto.UserDTO;
import com.sportswork.sportswork.core.service.impl.RoleServiceImp;
import com.sportswork.sportswork.core.service.impl.StudentServiceImp;
import com.sportswork.sportswork.core.service.impl.TeacherServiceImp;
import com.sportswork.sportswork.core.service.impl.UserServiceImp;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author dengwei
 * @date 2020/2/4 16:49
 * @description
 */
@Slf4j
@Controller("userEntityController")
public class UserController {
    @Resource
    private UserServiceImp userServiceImp;
    @Resource
    private RoleServiceImp roleServiceImp;
    @Resource
    private StudentServiceImp studentServiceImp;
    @Resource
    private TeacherServiceImp teacherServiceImp;

    @RequestMapping("/admin/user/list")
    public String List(){
        return "/pages/view/admin/user/list";
    }

    @RequestMapping("/admin/user/editpassword")
    public String editpassword(){
        return "/pages/view/admin/user/editpassword";
    }

    @RequestMapping("/admin/user/personal")
    //@PreAuthorize("hasAnyRole('admin')")
    public String personal(){
        return "/pages/view/admin/user/personal";
    }


    @RequestMapping("/admin/user/getAllUsers")
    @ResponseBody
    public Object getAllUsers(){
        return new PageDTO<UserDTO>().toPageDTO(
                UserDTO.UserDTOList(userServiceImp.getAllUsers()));
    }

    @RequestMapping(value = "/admin/user/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(UserDTO userDTO) {
        userServiceImp.addUser(userDTO);
        return "success";
    }


    @RequestMapping(value = "/admin/user/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(UserDTO userDTO) {
        userServiceImp.setUser(userDTO);
        return "success";
    }

    @RequestMapping("/admin/user/getAllRoles")
    @ResponseBody
    public Object getAllRoles() {
        return new PageDTO<Role>().toPageDTO(roleServiceImp.getAllRoles());
    }

    @RequestMapping(value = "/admin/user/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestParam String username) {
        userServiceImp.delUser(username);
        return "true";
    }

    @RequestMapping(value = "/admin/user/setPsw", method = RequestMethod.POST)
    @ResponseBody
    public String setPsw(String oldpsw , String newpsw) {
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        if(!oldpsw.equals(userDetails.getPassword())){
            return "false";
        }
        userServiceImp.setPassword(userDetails.getUserId(),newpsw,userDetails.getRoleId());
        return "true";
    }

    @RequestMapping(value = "/admin/user/getUserInfo")
    @ResponseBody
    public User getUserInfo(String oldpsw , String newpsw) {
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        return userServiceImp.getUser(userDetails.getUserId());
    }


    @RequestMapping(value = "/admin/user/setUsernames", method = RequestMethod.POST)
    @ResponseBody
    public String setUsernames(User user) {
        userServiceImp.setUsername(user);
        return "true";
    }

    @RequestMapping(value = "/admin/user/getStudent", method = RequestMethod.POST)
    @ResponseBody
    public Student getStudent() {
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        return  studentServiceImp.getStudent(userDetails.getUserId());
    }

    @RequestMapping(value = "/admin/user/editjs", method = RequestMethod.POST)
    @ResponseBody
    public String editjs(Teacher teacher) {
        teacherServiceImp.setTeacher(teacher);
        return "success";
    }



    @RequestMapping(value = "/admin/user/getTeacher", method = RequestMethod.POST)
    @ResponseBody
    public Teacher getTeacher() {
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        return  teacherServiceImp.getTeacher(userDetails.getUserId());
    }







    @RequestMapping(value = "/admin/user/stop", method = RequestMethod.POST)
    @ResponseBody
    public Object stop(@RequestBody User user, HttpServletRequest request, HttpSession session) {

        userServiceImp.stopUser(user);
        return "true";
    }









    @RequestMapping(value = "/admin/user/getUser", method = RequestMethod.POST)
    @ResponseBody
    public User getUser(String id,HttpServletRequest request) {
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String rolesName = userDetails.getRoleCode();
        return userServiceImp.getUser(userDetails.getUserId());
    }

    @RequestMapping(value = "/admin/user/editxs", method = RequestMethod.POST)
    @ResponseBody
    public String editxs(Student student) {
        studentServiceImp.setStudent(student);
        return "success";
    }







}
